package com.project.dao.impl;

import com.project.dao.StudentDao;
import com.project.entity.Student;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author wzm
 * @create 2023-05-31 9:22
 */
public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    public int insertStu(Student student) {
        String sql = "insert into student values(?,?,?,?,?,?)";
        return update(sql,0,student.getUsername(),student.getPassword(),student.getAge(),student.getPhone(),student.getStatus());
    }

    @Override
    public int deleteStu(Student student) {
        String sql = "delete from student where id = ?";
        return update(sql,student.getId());
    }

    @Override
    public int updateStu(Student student) {
        String sql = "update student set username = ?,password = ?,age = ?,phone = ?,status = ? where id = ?";
        return update(sql,student.getUsername(),student.getPassword(),student.getAge(),student.getPhone(),student.getStatus(),student.getId());
    }

    @Override
    public Student getStuById(Student student) {
        String sql ="select * from student where id = ?";
        return selectForOne(Student.class,sql,student.getId());
    }

    @Override
    public List<Student> getStuList() {
        String sql = "select * from student";
        return selectForList(Student.class,sql);
    }

    @Override
    public Student selectByName(Student student) {
        String sql = "select * from student where username = ?";
        return selectForOne(Student.class,sql,student.getUsername());
    }

    @Override
    public int deleteStuByIds(int[] ids) {
        String sql = "delete from student where id = ?";
        int count = 0;
        //循环遍历id同时触发删操作
        for (int i = 0; i < ids.length; i++) {
           count +=  update(sql,ids[i]);
        }

        return count;
    }

    @Override
    public List<Student> selectByPage(int begin, int pageSize) {
        System.out.println("begin = " + begin);
        System.out.println("pageSize = " + pageSize);
        String sql = "select * from student limit ?,?";
        return selectForList(Student.class,sql,begin,pageSize);
    }

    @Override
    public int selectByPageCount() {
        String sql ="select count(*) from student";
        Number number = (Number) selectForValue(sql);//准换
        return number.intValue();
    }

    @Override
    public List<Student> selectByPageIf(int begin, int pageSize, Student student) {

        //获取条件 判断username和status是否有值  有值就需要条件
        boolean isUserName = StringUtils.isNotBlank(student.getUsername());
        boolean isStatus =student.getStatus() != null;
        
        //使用三元运算符判断条件有无 组合sql的条件
        String if1 = isUserName ? "username like " : "1 = ";
        String if2 = isStatus ? "status = " : "1 = ";

        
        //通用sql有条件 无条件都可以适配
        //select * from student where username like ?(1 = ?) 
        // and status = ?(1 = ?) limit begin,size
        String sql = "select * from student where " + if1 +" ? and " + if2 +" ? limit ?,?";

        //参数也要根据条件的有无来决定 有   = %username%  无   = 1
        String param01 = isUserName ? "%" + student.getUsername() + "%" : "1";
        // 有  = status  无 =1
        int param02 = isStatus?student.getStatus():1;

        System.out.println("sql = " + sql);
        return selectForList(Student.class,sql,param01,param02,begin,pageSize);
    }

    @Override
    public int selectByPageCountIf(Student student) {
        //获取条件 判断username和status是否有值  有值就需要条件
        boolean isUserName = StringUtils.isNotBlank(student.getUsername());
        boolean isStatus =student.getStatus() != null;

        //使用三元运算符判断条件有无 组合sql的条件
        String if1 = isUserName ? "username like " : "1 = ";
        String if2 = isStatus ? "status = " : "1 = ";

        String sql = "select count(1) from student where "+ if1 +" ? and " + if2 +" ?";

        //参数
        String param01 = isUserName ? "%" + student.getUsername() + "%" : "1";
        // 有  = status  无 =1
        int param02 = isStatus?student.getStatus():1;

        Number number = (Number) selectForValue(sql, param01, param02);
        return number.intValue();
    }
}
